<?php
class RegisterController extends AppController
{
	var $autoRender = true;
    var $uses = array('Users','User_expand');
    var $layout = 'empty';
    
    function index()
    {
    	
    }
    
    /**
     * 生成验证码
     */
    function get_auth_code()
    {
    	@include_once( dirname(__FILE__).'/../../config/config.php' );
		header("Content-type:image/png");
		
		$img_height = 20;
		$img_width = 60;
		$im = @imagecreate( $img_width, $img_height );
		$background_color = imagecolorallocate( $im, 255, 255, 255 );
		$text_color = imagecolorallocate( $im, rand( 1, 121 ), rand( 1, 121 ), rand( 1, 121 ) );
		
		for($i=1;$i<100;$i++)
		{
			if ( $i % 2 == 0 )
			{
				$disturb_str = '+';
			}
			else
			{
				$disturb_str = '*';
			}
			imagestring( $im, 1, mt_rand( 1, $img_width ), mt_rand( 1, $img_height ), $disturb_str,
			imageColorAllocate( $im, mt_rand( 121, 255 ), mt_rand( 121, 255 ), mt_rand( 121, 255 ) ) );
		}
		
		while( ( $num = rand() % 10000 ) < 1000 );
		{
		}
		$_SESSION['check_code'] = $num;
		//echo $_SESSION['check_code'];
		imagestring( $im, 4, 15, 2, $num, $text_color );
		imagepng( $im );
		imagedestroy( $im );
  	  	exit;
    }
    
    function register()
    {
    	//print_r($this->params);
    	//帐号
    	$account = $this->params["form"]["register_account"];
    	//昵称
    	$register_ulike = $this->params["form"]["register_ulike"];
    	//性别
    	//$sex = $this->params["form"]["sex"];
    	//密码
    	$register_password = $this->params["form"]["register_password"];
    	//确认密码
    	$register_respassword = $this->params["form"]["register_respassword"];
    	//验证码
    	$check_code = $this->params["form"]["check_code"];
    	//是否同意验证
    	//$register_agreement = $this->params["form"]["register_agreement"];
    	
    	//验证注册信息
    	if(!(preg_match_all('/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/',$account,$mail)||preg_match_all('/^[1][3-8]+\\d{9}/',$account,$tle)))
    	{
    		echo '请填写正确的手机或邮箱'	;
    		exit;
    	}
    	
    	if(!preg_match_all("/^[\x{4e00}-\x{9fa5}a-zA-Z0-9_]+$/u",$register_ulike,$nick))
    	{
    		echo '昵称不符合规范';
    		exit;		
    	}
    	if(utf8_strlen($register_ulike)<5||utf8_strlen($register_ulike)>20)
    	{
    		echo '昵称不符合规范';
    		exit;
    	}	
//    	if($sex=='')
//    	{	
//			echo '未选择性别';
//			exit;		
//    	}
    	
    	if(!preg_match("/^[0-9a-z_!@#$%^&*()~+|]{6,214}$/i", $register_password))
    	{
    		echo '密码需要是字母或数字，最小6位，最大20位';
    		exit;			
    	}
    	
    	if(utf8_strlen($register_password)<6||utf8_strlen($register_password)>20)
    	{
    		echo '密码需要是字母或数字，最小6位，最大20位';
    		exit;
    	}
    	
    	if($register_password!=$register_respassword)
    	{
    		echo '2次输入的密码不一致';
    		exit;
    	}
    	
    	if($check_code!=$_SESSION['check_code'])
    	{
    		echo '验证码错误';
    		exit;	
    	}
    	
//    	if($register_agreement!='on')
//    	{
//    		echo '未同意协议';
//    		exit;
//    	}
    	
    	$user = $this->Users->findAll("account='$account'");
    	if(count($user)!=0)
    	{
    		echo '该帐号已被注册';
    		exit;
    	}
    	$add_data = array();
    	
    	$add_data["account"] = $account;
    	$add_data["nick_name"] = $register_ulike;
    	$add_data["pwd"] = md5($register_password);
    	$time = date("Y-m-d H:i:s");
    	$add_data["create_time"] = $time;
    	$add_data["update_time"] = $time;
    	
    	$ret = $this->Users->save($add_data);
    	if($ret==true)
    	{
	    	$insert_id = $this->Users->id;
	    	
	    	$user_expand_data = array();
	    	$user_expand_data["user_id"] = $insert_id;
	    	//$user_expand_data["sex"] = $sex;
	    	$user_expand_data["create_time"] = $time;
	    	$user_expand_data["update_time"] = $time;
	    	
	    	$this->User_expand->save($user_expand_data);
	    	$_SESSION["user_id"] = $insert_id;
    		$_SESSION["account"] = $account;
    		$_SESSION["nick_name"] =  $register_ulike;
    		$_SESSION['login_type'] = 'huanxinzu';
    		print_r($_SESSION);
    	}else{
    		echo '帐号注册失败';
    		exit;
    	}
    }
    
    function check_account($account)
    {
   		if(!(preg_match_all('/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/',$account,$mail)||preg_match_all('/^[1][3-8]+\\d{9}/',$account,$tle)))
    	{
    		$msg_ary = array('ret'=>1,'msg'=>'请填写正确的手机或邮箱');
    		echo json_encode($msg_ary);
    		exit;
    	}else{
	    	$user = $this->Users->findAll("account='$account'");
	    	if(count($user)!=0)
	    	{
	    		if(preg_match_all('/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/',$account,$mail))
	    		{
	    			echo json_encode(array('ret'=>1,'msg'=>'该电子邮箱已经绑定了其他账号，请重新填写'));
	    		}
	    		if(preg_match_all('/^[1][3-8]+\\d{9}/',$account,$tle))
	    		{
	    			echo json_encode(array('ret'=>1,'msg'=>'该手机号已经绑定了其他账号，请重新填写'));
	    		}
	    		exit;
	    	}else{
	    		echo json_encode(array('ret'=>0,'msg'=>'ok'));
	    		exit;
	    	}
	    }
    }
    
    function check_ulike($ulike)
    {
    	if(utf8_strlen($ulike)<5||utf8_strlen($ulike)>30)
    	{
    		echo json_encode(array('ret'=>0,'msg'=>'昵称不符合规范(支持中文,最多30个字符，中文算两个字符。)'));
    		exit;
    	}else{
    		echo json_encode(array('ret'=>0,'msg'=>'ok'));
    		exit;
    	}
    }
    
    function check_code($check_code)
    {
    	if($check_code!=$_SESSION['check_code'])
    	{
    		echo json_encode(array('ret'=>1,'msg'=>'验证码错误'));
    		exit;	
    	}else{
    		echo json_encode(array('ret'=>0,'msg'=>'ok'));
    		exit;
    	}
    }
    
    function check_pwd($register_password)
    {
   		 if(!preg_match("/^[0-9a-z_!@#$%^&*()~+|]{6,214}$/i", $register_password))
    	{
    		echo json_encode(array('ret'=>1,'msg'=>'密码需要是字母或数字，最小6位，最大20位'));
    		exit;			
    	}
    	
    	if(utf8_strlen($register_password)<6||utf8_strlen($register_password)>20)
    	{
    		echo json_encode(array('ret'=>1,'msg'=>'密码需要是字母或数字，最小6位，最大20位'));
    		exit;
    	}
    	
    	echo json_encode(array('ret'=>0,'msg'=>'ok'));
    	exit;
    }
}